import { MenuComponent } from '@/types/interface';
import { sort } from '@/soar';
import menusJson from './menus.json';

export function componentToPath(path: string) {
  return path.replace('pages/', '/').replace('/List.vue', '').replace('.vue', '');
}

/**
 * 获取布局配置
 */
export async function layouts(modules = import.meta.glob('@/layouts/schema.ts')) {
  const schemas = [];
  // 布局组件
  for (const key in modules) {
    // @ts-ignore
    if (modules.hasOwnProperty(key)) {
      // @ts-ignore 调用导入函数获取模块内容
      const layout = await modules[key]();
      let { COMPONENTS } = layout;
      if (!Array.isArray(COMPONENTS)) {
        COMPONENTS = [COMPONENTS];
      }
      for (let i = 0; i < COMPONENTS.length; i++) {
        // @ts-ignore
        const menuComponent: MenuComponent = COMPONENTS[i];
        const component = await menuComponent.component();
        const index = component.default.__file.indexOf('/src/');
        const path = component.default.__file.substring(index + 5);
        schemas.push({
          code: path.replace('.vue', ''),
          path,
          name: menuComponent.name,
          permits: menuComponent.permits,
          elements: [],
        });
      }
    }
  }
  return schemas;
}

export async function pages(modules = import.meta.glob(`@/pages/**/schema.ts`)) {
  const schemas = [];
  // 页面组件
  for (const key in modules) {
    // @ts-ignore
    if (modules.hasOwnProperty(key)) {
      // @ts-ignore 调用导入函数获取模块内容
      const module = await modules[key]();
      // @ts-ignore 假设每个模块都有一个叫做 myArray 的导出
      let { COMPONENTS } = module;
      if (COMPONENTS) {
        if (!Array.isArray(COMPONENTS)) {
          COMPONENTS = [COMPONENTS];
        }
        for (let i = 0; i < COMPONENTS.length; i++) {
          // @ts-ignore
          const menuComponent: MenuComponent = COMPONENTS[i];
          let component = await menuComponent.component();
          const index = component.default.__file.indexOf('/src/');
          component = component.default.__file.substring(index + 5);
          const path = menuComponent.path ? menuComponent.path : componentToPath(component);
          const code = menuComponent.code ? menuComponent.code : path;
          schemas.push({
            type: 1,
            title: menuComponent.title,
            path,
            code,
            component,
            icon: menuComponent.icon,
            keepalive: menuComponent.keepalive,
            sort: menuComponent.sort,
            children:
              menuComponent.elements?.map((x) => {
                return {
                  type: 0,
                  title: x.title,
                  code: /^[a-zA-Z0-9].*$/.test(x.code) ? `${code}/${x.code}` : x.code,
                  permits: x.permits,
                };
              }) ?? [],
            permits: menuComponent.permits,
          });
        }
      }
    }
  }
  return schemas;
}

/**
 * 生成菜单数据
 */
export async function getMenus() {
  if (import.meta.env.DEV) {
    const menus = [
      {
        type: 2,
        title: '仪表盘',
        path: '/dashboard',
        code: '/dashboard',
        redirect: '/dashboard/base',
        component: 'layouts/index.vue',
        icon: 'app',
        sort: 0,
        children: await pages(import.meta.glob(`@/pages/dashboard/*/schema.ts`)),
      },
      {
        type: 2,
        title: '系统管理',
        code: '/system',
        path: '/system',
        redirect: '/system/auth',
        component: 'layouts/index.vue',
        icon: 'app',
        sort: 100,
        children: [
          {
            type: 2,
            title: '授权中心',
            code: '/system/auth',
            path: '/system/auth',
            redirect: '/system/auth/user',
            component: '',
            icon: 'lock-on',
            sort: 100,
            children: await pages(import.meta.glob(`@/pages/system/auth/*/schema.ts`)),
          },
          {
            type: 2,
            title: '信息中心',
            code: '/system/message',
            path: '/system/message',
            redirect: '/system/message/notice',
            component: '',
            icon: 'chat',
            sort: 200,
            children: await pages(import.meta.glob(`@/pages/system/message/*/schema.ts`)),
          },
          {
            type: 2,
            title: '监控中心',
            code: '/system/monitor',
            path: '/system/monitor',
            redirect: '/system/monitor/log',
            component: '',
            icon: 'laptop',
            sort: 300,
            children: await pages(import.meta.glob(`@/pages/system/monitor/*/schema.ts`)),
          },
          {
            type: 2,
            title: '配置中心',
            code: '/system/general',
            path: '/system/general',
            redirect: '/sys/general/dict',
            icon: 'tools',
            sort: 400,
            children: await pages(import.meta.glob(`@/pages/system/general/*/schema.ts`)),
          },
          {
            type: 2,
            title: '租户中心',
            code: '/system/tenant',
            path: '/system/tenant',
            redirect: '/system/tenant/list',
            component: '',
            icon: 'user-avatar',
            sort: 500,
            children: await pages(import.meta.glob(`@/pages/system/tenant/*/schema.ts`)),
          },
          {
            type: 2,
            title: '单点登录',
            code: '/system/sso',
            path: '/system/sso',
            redirect: '/system/sso/sso-apply',
            component: '',
            icon: 'view-module',
            sort: 600,
            children: await pages(import.meta.glob(`@/pages/system/sso/*/schema.ts`)),
          },
        ],
      },
      {
        type: 2,
        title: '协同办公',
        code: '/flow',
        path: '/flow',
        redirect: '/flow/task',
        component: 'layouts/index.vue',
        icon: 'app',
        sort: 200,
        children: [
          {
            type: 2,
            title: '任务管理',
            code: '/flow/task',
            path: '/flow/task',
            redirect: '/flow/task/todo',
            icon: 'file-unknown',
            sort: 100,
            children: await pages(import.meta.glob(`@/pages/flow/task/*/schema.ts`)),
          },
          {
            type: 2,
            title: '流程管理',
            code: '/flow/process',
            path: '/flow/process',
            redirect: '/flow/process/manage',
            icon: 'file-unknown',
            sort: 200,
            children: await pages(import.meta.glob(`@/pages/flow/process/*/schema.ts`)),
          },
        ],
      },
      {
        type: 2,
        title: '内容管理',
        code: '/cms',
        path: '/cms',
        redirect: '/cms/article',
        component: 'layouts/index.vue',
        icon: 'folder-open',
        sort: 300,
        children: [
          {
            type: 2,
            title: '图文',
            code: '/cms/article',
            path: '/cms/article',
            redirect: '/cms/article/manager',
            icon: 'file-unknown',
            sort: 100,
            children: await pages(import.meta.glob(`@/pages/cms/article/*/schema.ts`)),
          },
          {
            type: 2,
            title: '音频',
            code: '/cms/audio',
            path: '/cms/audio',
            redirect: '/cms/audio/manager',
            icon: 'notification-filled',
            sort: 200,
            children: await pages(import.meta.glob(`@/pages/cms/audio/*/schema.ts`)),
          },
          {
            type: 2,
            title: '视频',
            code: '/cms/video',
            path: '/cms/video',
            redirect: '/cms/video/manager',
            component: '',
            icon: 'play-circle-filled',
            sort: 300,
            children: await pages(import.meta.glob(`@/pages/cms/video/*/schema.ts`)),
          },
          {
            type: 2,
            title: '其他',
            code: '/cms/other',
            path: '/cms/other',
            redirect: '/cms/other/region',
            component: '',
            icon: 'play-circle-filled',
            sort: 400,
            children: await pages(import.meta.glob(`@/pages/cms/other/*/schema.ts`)),
          },
        ],
      },
    ];
    sort(menus, 'sort');
    return menus;
  }
  return menusJson;
}
